package com.example.frickytest;


import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.content.Intent;
import android.view.View;

public class BMIActivity extends Activity {
	private Intent intent;
	private Bundle bunde;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.mylayout);
		
		intent = this.getIntent();
		bunde = this.getIntent().getExtras();

		String sex = bunde.getString("sex");
		double height = bunde.getDouble("height");

		String sexText = "";
		if (sex.equals("M")) {
			sexText = "男性";
		} else {
			sexText = "女性";
		} 
		
		String weight = this.getWeight(sex, height);

		TextView tv1 = (TextView) findViewById(R.id.text1);
		tv1.setText("你是一位" + sexText + "\n你的身高是" + height + "厘米\n你的标准体重是"
				+ weight + "公斤");
		
		Button b1 = (Button) findViewById(R.id.button_back);
		b1.setOnClickListener(new Button.OnClickListener() {

		public void onClick(View v) {
				// TODO Auto-generated method stub
				/* 返回result 回上一个activity */
				BMIActivity.this.setResult(RESULT_OK, intent);
				/* 结束这个activity */
				BMIActivity.this.finish();
			}
		});
	}


	private String format(double num) {
		NumberFormat formatter = new DecimalFormat("0.00");
		String s = formatter.format(num);
		return s;
	}


	private String getWeight(String sex, double height) {
		String weight = "";
		if (sex.equals("M")) {
			weight = format((height - 80) * 0.7);
		} else {
			weight = format((height - 70) * 0.6);
		}
		return weight;
	}
}